package site.hanzhe.cnCalendar;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.chinese.GanZhi;
import cn.hutool.core.date.chinese.SolarTerms;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import site.hanzhe.cnCalendar.pojo.CnCalendar;
import site.hanzhe.cnCalendar.utils.CnGanZhi;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class CnCalendarMain {

    // 1901年02月04日
    public static void main(String[] args) {
        // 从1901-01-01开始推算，年份是庚子年
        CnGanZhi startYearGZ = new CnGanZhi("庚子");
        LocalDate startLocalDate = LocalDate.of(1901, 1, 1);
        LocalDate endLocalDate = LocalDate.of(2099, 12, 31);
        List<CnCalendar> cnCalendarList = new ArrayList<>();
        while (true) {
            CnCalendar cnCalendar = new CnCalendar();
            ChineseDate chineseDate = new ChineseDate(startLocalDate);
            // 公历年月日
            cnCalendar.setYear(startLocalDate.getYear());
            cnCalendar.setMonth(startLocalDate.getMonthValue());
            cnCalendar.setDayOfMonth(startLocalDate.getDayOfMonth());
            cnCalendar.setDayOfWeek(startLocalDate.getDayOfWeek().getValue());
            // 生肖、是否闰月、节气
            cnCalendar.setZodiac(chineseDate.getChineseZodiac());
            cnCalendar.setLeapMonth(chineseDate.isLeapMonth());
            cnCalendar.setSolarTerm(SolarTerms.getTerm(cnCalendar.getYear(), cnCalendar.getMonth(), cnCalendar.getDayOfMonth()));

            // 农历年、农历月、农历日、中文农历年
            cnCalendar.setLunarYearCn(GanZhi.getGanzhiOfYear(chineseDate.getChineseYear()));
            cnCalendar.setLunarYear(chineseDate.getChineseYear());
            cnCalendar.setLunarMonth(chineseDate.getMonth() - (cnCalendar.isLeapMonth() ? 1 : 0));
            cnCalendar.setLunarDay(chineseDate.getDay());

            // 年干支、月干支、日干支
            String yearGz = "立春".equals(cnCalendar.getSolarTerm()) ? startYearGZ.nextAndGet() : startYearGZ.get();
            String monthGz = GanZhi.getGanzhiOfMonth(cnCalendar.getYear(), cnCalendar.getMonth(), cnCalendar.getDayOfMonth());
            String dayGz = GanZhi.getGanzhiOfDay(cnCalendar.getYear(), cnCalendar.getMonth(), cnCalendar.getDayOfMonth());
            cnCalendar.setYearGan(yearGz.substring(0, 1));
            cnCalendar.setYearZhi(yearGz.substring(1, 2));
            cnCalendar.setMonthGan(monthGz.substring(0, 1));
            cnCalendar.setMonthZhi(monthGz.substring(1, 2));
            cnCalendar.setDayGan(dayGz.substring(0, 1));
            cnCalendar.setDayZhi(dayGz.substring(1, 2));

            cnCalendarList.add(cnCalendar);
            if (startLocalDate.equals(endLocalDate)) break;
            startLocalDate = startLocalDate.plusDays(1);
        }

        // 将list转换为JSON集合，保存到JSON文件中
        String jsonStr = JSONUtil.toJsonStr(cnCalendarList);
        FileUtil.writeString(jsonStr, "D:/cn_calendar.min.json", StandardCharsets.UTF_8);
        System.out.println("执行完成");
    }

}
